#! /usr/bin/perl

use warnings;
use strict;

if ($ARGV[0] eq '-h') {
    print 
        "Usage: $0 [-d]\n",
        "Read SQL keywords from stdin and generate ANTLR grammar\n",
        "fragments, which parses them case insensitively.\n",
        "  -d: Print default keywords.\n";
    exit;
}

my $input = $ARGV[0] eq '-d' ? *DATA{IO} : *STDIN{IO};

my @keywords;
while (<$input>) { push @keywords, $1 if /(\S+)/; }

my $max = 0;
foreach (@keywords) {
    $max = length if length > $max;
}

foreach (@keywords) {
    print
        "$_:", " " x ($max +1 -length),
        join (' ', map { /_/ ? 'UNDERSCORE' : $_ } split ('')),
        ";\n";
}

foreach ('A'..'Z') {
    print "fragment $_: ('", lc, "' | '$_');\n";
}

__END__
ABSOLUTE
ACTION
ADA
ADD
ALL
ALLOCATE
ALTER
AND
ANY
ARE
AS
ASC
ASSERTION
AT
AUTHORIZATION
AVG
BEGIN
BETWEEN
BIT
BIT_LENGTH
BOTH
BY
C
CASCADE
CASCADED
CASE
CAST
CATALOG
CATALOG_NAME
CHAR
CHARACTER
CHARACTER_LENGTH
CHARACTER_SET_CATALOG
CHARACTER_SET_NAME
CHARACTER_SET_SCHEMA
CHAR_LENGTH
CHECK
CLASS_ORIGIN
CLOSE
COALESCE
COBOL
COLLATE
COLLATION
COLLATION_CATALOG
COLLATION_NAME
COLLATION_SCHEMA
COLUMN
COLUMN_NAME
COMMAND_FUNCTION
COMMIT
COMMITTED
CONDITION_NUMBER
CONNECT
CONNECTION
CONNECTION_NAME
CONSTRAINT
CONSTRAINTS
CONSTRAINT_CATALOG
CONSTRAINT_NAME
CONSTRAINT_SCHEMA
CONSTRATIN_CATALOG
CONTINUE
CONVERT
CORRESPONDING
COUNT
CREATE
CROSS
CURRENT
CURRENT_DATE
CURRENT_TIME
CURRENT_TIMESTAMP
CURRENT_USER
CURSOR
CURSOR_NAME
DATA
DATE
DATETIME_INTERVAL_CODE
DATETIME_INTERVAL_PRECISION
DAY
DEALLOCATE
DEC
DECIMAL
DECLARE
DEFAULT
DEFERRABLE
DEFERRED
DELETE
DESC
DESCRIBE
DESCRIPTOR
DIAGNOSTICS
DISCONNECT
DISTINCT
DOMAIN
DOUBLE
DOUBLE_PRECISION
DROP
DYNAMIC_FUNCTION
ELSE
END
END-EXEC
ESCAPE
EXCEPT
EXCEPTION
EXEC
EXECUTE
EXISTS
EXTERNAL
EXTRACT
FALSE
FETCH
FIRST
FLOAT
FOR
FOREIGN
FORTRAN
FOUND
FROM
FULL
GET
GLOBAL
GO
GOTO
GRANT
GROUP
HAVING
HOUR
High
IDENTITY
IMMEDIATE
IN
INDICATOR
INDICATOR_TYPE
INITIALLY
INNER
INPUT
INSENSITIVE
INSERT
INT
INTEGER
INTERSECT
INTERVAL
INTO
IS
ISOLATION
IntegrityNo
IntegrityYes
Intermediate
JOIN
KEY
LANGUAGE
LAST
LEADING
LEFT
LENGTH
LEVEL
LIKE
LOCAL
LOWER
Low
MATCH
MAX
MESSAGE_LENGTH
MESSAGE_OCTET_LENGTH
MESSAGE_TEXT
MIN
MINUTE
MODULE
MONTH
MORE
MUMPS
NAME
NAMES
NATIONAL
NATURAL
NCHAR
NEXT
NO
NOT
NULL
NULLABLE
NULLIF
NUMBER
NUMERIC
OCTET_LENGTH
OF
ON
ONLY
OPEN
OPTION
OR
ORDER
OUTER
OUTPUT
OVERLAPS
PAD
PARTIAL
PASCAL
PLI
POSITION
PRECISION
PREPARE
PRESERVE
PRIMARY
PRIOR
PRIVILEGES
PROCEDURE
PUBLIC
READ
REAL
REFERENCES
RELATIVE
REPEATABLE
RESTRICT
RETURNED_LENGTH
RETURNED_OCTET_LENGTH
RETURNED_SQLSTATE
REVOKE
RIGHT
ROLLBACK
ROWS
ROW_COUNT
SCALE
SCHEMA
SCHEMA_NAME
SCROLL
SECOND
SECTION
SELECT
SERIALIZABLE
SERVER_NAME
SESSION
SESSION_USER
SET
SIZE
SMALLINT
SOME
SPACE
SQL
SQLCODE
SQLCODE_TYPE
SQLERROR
SQLSTATE
SQLSTATE_TYPE
SQL_STANDARD
SUBCLASS_ORIGIN
SUBSTRING
SUM
SYSTEM_USER
TABLE
TABLE_NAME
TEMPORARY
THEN
TIME
TIMESTAMP
TIMEZONE_HOUR
TIMEZONE_MINUTE
TO
TRAILING
TRANSACTION
TRANSLATE
TRANSLATION
TRIM
TRUE
TYPE
UNCOMMITTED
UNION
UNIQUE
UNKNOWN
UNNAMED
UPDATE
UPPER
USAGE
USER
USING
VALUE
VALUES
VARCHAR
VARYING
VIEW
WHEN
WHENEVER
WHERE
WITH
WORK
WRITE
YEAR
ZONE
